package org.example.leetcode;

import java.util.*;
/**
 * @author: lynn
 * @Descript:
 * @date: 2021/8/5 11:15
 * @version: 1.0
 */
public class LC49 {
    public static void main(String[] args) {
        String[] strs={"eat", "tea", "tan", "ate", "nat", "bat"};
        LC49 lc=new LC49();
        System.out.println(lc.groupAnagrams(strs));
    }

    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> res=new ArrayList<>();
        if (strs==null || strs.length==0) return res;

        Map<String,List<String>> map=new HashMap<>();
        for(String c:strs){
            List<String> list=new ArrayList<>();
            String sortKey=sortKey(c);
            list=map.get(sortKey);
            if (list==null){
                list=new ArrayList<>();
            }
            list.add(c);
            map.put(sortKey,new ArrayList<>(list));
        }
        //fixme map遍历，这种方式遍历出来的key，是一个个键值对 map.entrySet()
        for (Map.Entry<String,List<String>> key:map.entrySet()) {
            res.add(key.getValue());
        }
        return res;
    }
    //fixme 字符串排序
    private String sortKey(String c){
        char[] chars=c.toCharArray();
        Arrays.sort(chars);
        return Arrays.toString(chars);
    }
}
